<?php
/**
 * 字符串下标索引
 * 一.如何声明
 * 下标格式: 
 * 1.字符串类型不转换 
 * 2.整型字符串转换为整型
 * 3.浮点数转换为整型 
 * 4.浮点数字符串不转换
 * 二.如何在字符串调用key
 */
// Show all errors
error_reporting(E_ALL);

$br = "<br/>";
echo '下标格式: 1.字符串类型不转换 2.整型字符串转换为整型,3.浮点数转换为整型 4.浮点数字符串不转换', $br;
$arr = [1.6 => 2, '1.5' => 3, '5' => 'hi'];
$i = 1.0;
$i = ceil($i);
var_dump($arr, $i);

/**
 * 字符串下标索引 如何使用
 */
$arr = ['name' => 'limit', 'sex' => 'man', 'age' => 12];
$arr_str = [
    "$arr[name]",      //okey
    //"$arr['name']",  //Incorrect syntax error
    "{$arr['name']}",  //okey
    //"{$arr[name]}",  //Incorrect Use of undefined constant name
];
var_dump($arr_str);

$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// Correct
print $arr['fruit'];  // apple
print $arr['veggie']; // carrot

// Incorrect.  This works but also throws a PHP error of level E_NOTICE because
// of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
#print $arr[fruit];    // apple

// This defines a constant to demonstrate what's going on.  The value 'veggie'
// is assigned to a constant named fruit.
#define('fruit', 'veggie');

// Notice the difference now
#print $arr['fruit'];  // apple
#print $arr[fruit];    // carrot


// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]";      // Hello apple

// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
#print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr['fruit']}";  // Hello apple

// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
#print "Hello $arr['fruit']";
#print "Hello $_GET['foo']";

// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple

echo  '获取数组的第一个元素和最后一个元素' , $br;
$arr = [1, 2, 3, 4, 5, 6];
var_dump(reset($arr), end($arr));


$arr = array(1,2,3, '1.0' => 4);
var_dump($arr, $arr['1.0']);
echo "<br/>";

$arr = array(
    (string)1 => '到店',
    (string)2 => '上门',
    (string)3 => '在线',
    "3" => '电话',
);
var_dump($arr, array_keys($arr));
